package com.hamizhong.iflowers.web.action.admin.orders;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.ICityService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.service.IOrdersService;
import com.hamizhong.iflowers.service.IProvinceService;
import com.hamizhong.iflowers.utils.MyColumnIndicator;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.AdminVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.OrdersVO;

public class AjaxQueryOrdersByStateAction extends MyBaseAjaxAction {
	private IOrdersService iOrdersService = null;
	private IProvinceService iProvinceService = null;
	private ICityService iCityService = null;
	private IGoodsService iGoodsService = null;
	public IOrdersService getiOrdersService() {
		return iOrdersService;
	}
	public void setiOrdersService(IOrdersService iOrdersService) {
		this.iOrdersService = iOrdersService;
	}
	public IProvinceService getiProvinceService() {
		return iProvinceService;
	}
	public void setiProvinceService(IProvinceService iProvinceService) {
		this.iProvinceService = iProvinceService;
	}
	public ICityService getiCityService() {
		return iCityService;
	}
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryOrdersByStateAction...");
		String orderState = getRQ().getParameter("orderState");
		String pageNum = getRQ().getParameter("pageNum");
		int pageSize = MyConstants.PAGE_SIZE_COMMON_ADMIN_ORDERS;
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		AdminVO adminVO = (AdminVO) getSSN().getAttribute(MyConstants.IFLOWERS_LOGIN_ADMIN_VO);
		if(adminVO == null) {
			jo.put("msg", MyErrorMsgConstants.ERR_LOGIN);
			jo.put("result", MyConstants.AJAX_FAILED_LOGIN);
			return jo;
		}
		IOPResultDto<IObjectVO> dto = iOrdersService.findByState(Integer.valueOf(orderState) , Integer.valueOf(pageNum), Integer.valueOf(pageSize));
		
		int totalOrdersCnt = dto.getFirstResult().getTotalOrdersCnt();
		//设置总条数
		jo.put("totalCnt", totalOrdersCnt+"");
		//设置当前页
		jo.put("pageNum", pageNum);
		//设置总页数
		jo.put("totalPageCnt", MyTools.convertItemCnt2PageCnt(totalOrdersCnt, MyConstants.PAGE_SIZE_COMMON_ADMIN_ORDERS)+"");
		
		List<OrdersVO> ordersVOList = new ArrayList<OrdersVO>();
		if(dto.isSuccess()) {
			ordersVOList = dto.getFirstResult().getOrdersVOList();
			for (OrdersVO ordersVO : ordersVOList) {
				jobj = new JSONObject();
				jobj.put("orderId", ordersVO.getOrderId());
				jobj.put("orderNum", ordersVO.getOrderNum());
				jobj.put("orderPrice", ordersVO.getOrderPrice());
				jobj.put("orderState", ordersVO.getOrderState());
			/*	if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_A) {
					jobj.put("orderStateStr", "新订单");
				}else if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_B) {
					jobj.put("orderStateStr", "已发货");
				}else if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_C) {
					jobj.put("orderStateStr", "已签收");
				}else if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_D) {
					jobj.put("orderStateStr", "已完成");
				}else if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_BACK_A) {
					jobj.put("orderStateStr", "用户退订中");
				}else if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_BACK_C) {
					jobj.put("orderStateStr", "用户退订完成");
				}else if(ordersVO.getOrderState() == MyColumnIndicator.O_ST_BACK_B) {
					jobj.put("orderStateStr", "管理员退订完成");
				}*/
				jobj.put("orderStateStr", MyColumnIndicator.O_ST_DESC_MAP.get(ordersVO.getOrderState()));
				jobj.put("goodsId", ordersVO.getGoodsVO().getGoodsId());
				jobj.put("goodsPictureS", ordersVO.getGoodsVO().getGoodsPictureS());
				jobj.put("goodsName", ordersVO.getGoodsVO().getGoodsName());
				jobj.put("goodsCnt", ordersVO.getGoodsCnt());
				jobj.put("cityId", ordersVO.getCity().getCityId());
				jobj.put("zip", ordersVO.getZip());
				jobj.put("consigneeName", ordersVO.getConsigneeName());
				jobj.put("consigneePhone", ordersVO.getConsigneePhone());
				jobj.put("consigneeAddr", ordersVO.getConsigneeAddr());
				jobj.put("remarks", ordersVO.getRemarks());
				jobj.put("createDate", MyTools.formatDttm2YYYY_MM_DD_HH_MM_SS(ordersVO.getCreateDate()));
				jobj.put("doneDate", MyTools.formatDttm2YYYY_MM_DD_HH_MM_SS(ordersVO.getDoneDate()));
				ja.add(jobj);
			}
			jo.put("data", ja.toString());
			jo.put("result", MyConstants.AJAX_SUCCESS);
			return jo;
		}else {
			jo.put("msg", MyErrorMsgConstants.FAILED_TO_QUERY_ORDERS);
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
			return jo;
		}
	}

}
